/**
 * @author temsa
 */

doh.registerGroup("tests.components",[
    function testRequireComponentWithCallback() {
        var d = new doh.Deferred();
        var url = $U("Archetype.unitTest.components.testComponent");
        var callbackParameter=null;
		
        var checkCondition =  function() {
            doh.assertNotEqual(callbackParameter,null, "Null Callback answer.");
            doh.assertNotEqual(Archetype.Component.get(url),null, "Component not stored !");
            doh.assertEqual(Archetype.Component.get(url), callbackParameter, "The component delivered by the callback is not identical to the one stored in Components (alternatively, the component may not have been loaded right now, you may consider to increase the wait time)");
            new Archetype.Component.get(url)();
        };
        
        Archetype.requireComponent(url, function(component){
            callbackParameter=component;
            Logger.debug(component);
        });
        setTimeout(d.getTestCallback(checkCondition), 200);
        return d;
    },

    function testRequireComponent2TimesWithCallback() {
        var d = new doh.Deferred();
        var module = "Archetype.unitTest.components.testComponentCallback";
        var url=$U(module);
        var called = 0;
        callback= function(component){
            called++;
            doh.assertEqual(Archetype.Component.get(url),component);
            new component(called);
        };

        Archetype.requireComponent(module, callback);

        var checkCondition = function() {
            var d2 = new doh.Deferred();
            doh.assertEqual(1, called);
            doh.assertNotEqual(Archetype.Component.get(url), null, "Component not stored !");
            Archetype.requireComponent(module, callback);

            var checkCondition2 = function() {
                doh.assertEqual(2, called);
            }
            setTimeout(d2.getTestCallback(checkCondition2), 200);
        };
        setTimeout(d.getTestCallback(checkCondition), 200);
        return d;
    },

    function testRequireComponentByModule() {
        var d = new doh.Deferred();
        var module = $U("Archetype.unitTest.components.testComponent");
        var url= $U(module);
        Archetype.requireComponent(module);
		
        var checkCondition =  function() {
            doh.assertNotEqual(Archetype.Component.get(url), null, "Component not stored !");
            new Archetype.Component.get(url)();
        }
        setTimeout(d.getTestCallback(checkCondition), 200);
        return d;
    },
	
    function testRequireComponentByUrl() {
        var d = new doh.Deferred();
        var url = $U("Archetype.unitTest.components.testComponent");
        Archetype.requireComponent(url);
		
        var checkCondition = function() {
            doh.assertNotEqual(Archetype.Component.get(url), null, "Component not stored !");
            new Archetype.Component.get(url)();
        }
        setTimeout(d.getTestCallback(checkCondition), 200);
        return d;
    }
    ]);
